<?php

function EJIsAPIParamValid(&$params, $aid) {
    $paramCount = W3GetAPIParamCount($aid);
    
    # The params is from reg check, so the first one is always the whole string be checked
    if (!isset($params) or sizeof($params) != $paramCount + 1) {
        W3LogWarning("API " . $aid . " parameters are not valid: " . var_dump($params));
        return false;
    }

    return true;
}

function EJIsAPIAuthenticated($aid, &$params, &$authResult) {
    # The params is from reg check, so the first one is always the whole string be checked
    $session = $params[W3GetAPIParamIndex($aid, w3SessionKey) + 1];
    if (!EJIsLogin($session)) {
        return false;
    }

    $authResult = $session;
    return true;
}

function EJIsAPIFamilyValid($session, &$fid) {
    $fid = EJGetLoginFamily($session);
    if ($fid == null) {
        W3LogWarning("Failed to get family ID");
        return false;
    }

    return true;
}

function EJIsPersonInFamily($pid, $fid) {
    $sql = "select person.ID as id from person where person.FID=" . $fid .
         " and " .
         "person.ID = " . $pid;
    $isInFamily = false;
    EJReadTable($sql, function ($row) use (&$isInFamily, $pid) {
        $isInFamily = true;
    });

    return $isInFamily;
}

function EJGetPersonInfoInFamily($fid) {
    $sql = "select person.ID as id, person.Name as name from person where person.FID=" . $fid;
    $results = array();
    EJReadTable($sql, function ($row) use (&$results) {
        array_push($results, array("ID" => $row["id"], "Name" => $row["name"]));
    });

    return $results;
}

function EJGetPersonInfoByName($personName) {
    $sql = "select " .
         "person.ID, person.FID" .
         " from " .
         "person" .
         " where " .
         "person.Name = " . W3MakeString($personName);

    $personInfo = array();
    EJReadTable($sql, function ($row) use (&$personInfo) {
        $personInfo["ID"] = $row["ID"];
        $personInfo["FID"] = $row["FID"];
    });

    return $personInfo;
}

function EJGetEventInfo($eventName, $eventID) {
    $condition = "";
    if ($eventName != null) {
        $condition = "Name = " . W3MakeString($eventName);
    } else if ($eventID != null) {
        $condition = "ID = " . W3MakeString($eventID);
    } else {
        W3LogWarning("Neither of event name nor id is valid when try to get event info");
        return array();
    }

    $sql = "select " .
         "ID, FID, Name, Budget" .
         " from " .
         "financeevent" .
         " where " . $condition;

    $eventInfo = array();
    EJReadTable($sql, function ($row) use (&$eventInfo) {
        $eventInfo["ID"] = $row["ID"];
        $eventInfo["FID"] = $row["FID"];
        $eventInfo["Name"] = $row["Name"];
        $eventInfo["Budget"] = $row["Budget"];
    });

    return $eventInfo;
}

function EJExecuteAPI($aid, &$params, $executor, $checkSession, $checkFamily) {

    if (!EJIsAPIParamValid($params, $aid)) {
        return W3CreateFailedResult();
    }

    if (!$checkSession) {
        return $executor($aid, $params);
    }

    $session = null;
    if (!EJIsAPIAuthenticated($aid, $params, $session)) {
        return W3CreateAuthenticationResult();
    }

    if (!$checkFamily) {
        return $executor($session, $aid, $params);
    }

    $fid = null;
    if (!EJIsAPIFamilyValid($session, $fid)) {
        return W3CreateFailedResult();
    }

    return $executor($fid, $aid, $params);
}

function EJExecuteWithAuthentication($aid, &$params, $executor) {
    return EJExecuteAPI($aid, $params, $executor, true, false);
}

function EJExecuteWithAuthenticatedFamily($aid, &$params, $executor) {
    return EJExecuteAPI($aid, $params, $executor, true, true);
}

function EJDecodeURLString($str) {
    # Replace White Space
    return str_replace("%20", " ", $str);
}

function EJConvertEventStringToIDs($eventsStr, $fid) {
    $results = array();
    if (trim($eventsStr) == "") {
        return $results;
    }

    // Support multiple events separated by ","
    $eventsArray = explode(",", $eventsStr);
    foreach ($eventsArray as $value) {
        $event = trim($value);
        $eventSql = "select financeevent.ID from financeevent where financeevent.Name like " .
                  W3MakeSingleQuotString($event) .
                  " and financeevent.FID=" . $fid;

        EJReadTable($eventSql, function ($row) use (&$results) {
                $eventId = $row["ID"];
                array_push($results, $eventId);
        });
    }

    return $results;
}

 ?>
